//
//  ShowLyric.m
//  ShowLyric
//
//  Created by qianfeng on 16/1/22.
//  Copyright (c) 2016年 qianfeng. All rights reserved.
//
/**
 *
 *  初始化时设置背景图片,歌词本地路径，歌曲总时长
 *  
 *  然后更新当前时间
 *
 */

#import "ShowLyricView.h"
#import "ShowLyricCell.h"
#import "LyricManager.h"
#import "Lyrics.h"

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface ShowLyricView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong)UIImageView * imageView;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,assign)NSTimeInterval flag_interval;

@end

@implementation ShowLyricView

#pragma mark - 预加载数据
-(void)loadPlay{
    
    self.index = 0;
    self.currentTime = 0;
    self.tableView.contentOffset = CGPointMake(0, -SCREEN_HEIGHT/2);
    self.flag_interval = 0;
    [self addDataSource];
    [self createBgImage];   //背景层
    [self addSubview:self.tableView];   //歌词层
    [self addGradientLayerWithColors];  //蒙版层
}

#pragma mark - 重写setter方法
-(void)setCurrentTime:(NSTimeInterval)currentTime{
    
    _currentTime = currentTime;
    [self refreshUI];
}

#pragma mark - 滚动视图
-(void)refreshUI{
    NSLog(@"%d",self.index);
    //播放完成
    if (_currentTime == self.duration)
        
        return;

    BOOL flag = (self.flag_interval <= _currentTime);
    if (self.index == (flag?self.dataSource.count:0))
        self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y + (flag?1:-1));
    else{
        Lyrics * model = self.dataSource[self.index+(flag?0:-1)];
        if (flag?(model.timeInterval - 1 < _currentTime):(model.timeInterval - 1 > _currentTime)) {
         
            [UIView animateWithDuration:1 animations:^{
                self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y + (flag?44:-44));
                self.index += (flag?1:-1);
            }];
        }
    }
    self.flag_interval = _currentTime;
    
//    //向后滚动
//    if (self.flag_interval <= _currentTime) {
//        
//        if (self.index == self.dataSource.count)
//            self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y + 1);
//        else{
//            Lyrics * model = self.dataSource[self.index];
//            if (model.timeInterval - 1 < _currentTime) {
//                
//                [UIView animateWithDuration:1 animations:^{
//                    self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y + 44);
//                    self.index++;
//                }];
//            }
//        }
//    }
//    //向前滚动
//    else{
//         if (self.index == 0)
//            self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y - 1);
//         else{
//            Lyrics * model = self.dataSource[self.index - 1];
//            if (model.timeInterval - 1 > _currentTime) {
//                [UIView animateWithDuration:1 animations:^{
//                    self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y - 44);
//                    self.index--;
//                }];
//            }
//        }
//    }
//    self.flag_interval = _currentTime;
}
#pragma mark - 创建背景层
-(void)createBgImage{
    // 创建imageView
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.imageView.image = self.bgImage;
    [self addSubview:self.imageView];
}
#pragma mark - 创建蒙版层
- (void)addGradientLayerWithColors{
    // 创建imageView
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.imageView.image = self.bgImage;
    [self addSubview:self.imageView];
    
    // 创建蒙版
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.colors = @[(__bridge id)[UIColor blackColor].CGColor,
                             (__bridge id)[UIColor clearColor].CGColor,
                             (__bridge id)[UIColor blackColor].CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0.1);
    gradientLayer.endPoint = CGPointMake(0, 0.9);
    [self.layer addSublayer:gradientLayer];
    
    UIView *containerView = [[UIView alloc] initWithFrame:self.imageView.bounds];
    [containerView.layer addSublayer:gradientLayer];
    self.imageView.maskView = containerView;
}

#pragma mark - 加载数据源
-(void)addDataSource{
    
    [LyricManager readAndParseFile:self.path andLyric:^(NSArray *lyricArr) {
        
        self.dataSource = [NSMutableArray arrayWithArray:lyricArr];
    }];
}

#pragma mark - 设置TableView显示多行歌词
-(UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.userInteractionEnabled = NO;
        _tableView.contentInset = UIEdgeInsetsMake(self.bounds.size.height/2-22, 0, self.bounds.size.height/2-22,0);
        
        [_tableView registerNib:[UINib nibWithNibName:@"ShowLyricCell" bundle:nil] forCellReuseIdentifier:@"cellId"];
    }
    return _tableView;
}

#pragma mark - TableView的协议方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ShowLyricCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
    Lyrics * lyric = self.dataSource[indexPath.row];
    cell.lyricLabel.text = lyric.lrc;
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
